package com.ib.webapp.controller;

import com.ib.model.Simulator;
import com.ib.service.GenericManager;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * Created by IMAM on 12/18/14.
 */
@Controller
@RequestMapping("/simulatorform*")
public class SimulatorFormController extends BaseFormController {
    private GenericManager<Simulator, Long> simulatorManager = null;

    @Autowired
    public void setSimulatorManager(@Qualifier("simulatorManager") GenericManager<Simulator, Long> simulatorManager) {
        this.simulatorManager = simulatorManager;
    }

    public SimulatorFormController() {
        setCancelView("redirect:simulators");
        setSuccessView("redirect:simulators");
    }

    @ModelAttribute
    @RequestMapping(method = RequestMethod.GET)
    protected Simulator showForm(HttpServletRequest request)
            throws Exception {
        String id = request.getParameter("id");

        if (!StringUtils.isBlank(id)) {
            return simulatorManager.get(new Long(id));
        }

        return new Simulator();
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(Simulator simulator, BindingResult errors, HttpServletRequest request,
                           HttpServletResponse response)
            throws Exception {
        if (request.getParameter("cancel") != null) {
            return getCancelView();
        }

        if (validator != null) { // validator is null during testing
            validator.validate(simulator, errors);

            if (errors.hasErrors() && request.getParameter("delete") == null) { // don't validate when deleting
                return "simulatorform";
            }
        }

        log.debug("entering 'onSubmit' method...");

        boolean isNew = (simulator.getId() == null);
        String success = getSuccessView();
        Locale locale = request.getLocale();

        if (request.getParameter("delete") != null) {
            simulatorManager.remove(simulator.getId());
            saveMessage(request, getText("simulator.deleted", locale));
        } else {
            simulatorManager.save(simulator);
            String key = (isNew) ? "simulator.added" : "simulator.updated";
            saveMessage(request, getText(key, locale));

            if (!isNew) {
                success = "redirect:simulatorform?id=" + simulator.getId();
            }
        }

        return success;
    }
}
